package com.streamer.app.utils

import android.app.Activity

object ActivityUtils {
    private val activitys = mutableListOf<Activity>()
    fun addActivity(activity: Activity) {
        activitys.add(activity)
    }

    fun removeActivity(activity: Activity) {
        activitys.remove(activity)
    }

    fun getTopActivity(): Activity? {
        if (!activitys.isNullOrEmpty())
            return activitys.last()

        return null
    }

    fun finishAllActivity() {
        activitys.forEach {
            if (it != null && !it.isFinishing) {
                it.finish()
            }
        }
        activitys.clear()
    }

    /**
     * 重建所有activity,排除传入得activity
     */
    fun recreateAllActivity(activity: Activity) {
        activitys.forEach {
            if (it != activity)
                it.recreate()
        }
    }

    fun randomString(length: Int): String {
        val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        return (1..length)
            .map { chars.random() }
            .joinToString("")
    }



}